package com.scoressystem.controller;

import com.scoressystem.pojo.Subjects;
import com.scoressystem.service.SubjectService;
import com.scoressystem.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Subject")
public class SubjectController {
    @Autowired
    SubjectService subjectService;
    @RequestMapping("/getAllSubject")
    public Results<List<Subjects>> getAllSubjectPage(@RequestParam(value="name",defaultValue = "")String Name,
                                                    @RequestParam(value = "page",defaultValue = "1") int pageIndex,
                                                    @RequestParam(value = "limit",defaultValue = "5") int pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("name", "%"+Name+"%");
        map.put("startPage", (pageIndex-1)*pageSize);
        map.put("pageSize", pageSize);
        List<Subjects> students=subjectService.SelectAllSubjectBypage(map);
        long total=subjectService.SelectAlSubjectsCount(map);
        Results<List<Subjects>> results=new Results("0","查询成功",students,total);
        return results;

    }
    @RequestMapping("/addSubject")
    @ResponseBody
    public Results addSubject(Subjects subjects) {

        Integer count1 = subjectService.searchSubjectBySubjectName(subjects);
        if (count1 > 0) {
            Results results = new Results("1", "新增失败，课程已存在");
            return results;

        } else {
           subjectService.insertSubjects(subjects);
            Results results = new Results("0", "新增成功");
            return results;
        }
    }
    @RequestMapping("/updateSubject")
    @ResponseBody
    public Results updateSubject(Subjects subjects) {
        int count1 = subjectService.updateSubject(subjects);
        if (count1 > 0) {
            Results results=new Results("0","更新成功");
            return results;
        }
        else {
            Results results=new Results("1","更新失败");
            return results;

        }
    }
    @RequestMapping("/deleteSubject")
    @ResponseBody
    public Results deleteStudent(Subjects subjects) {
        int count=  subjectService.deleteById(subjects);
        if (count > 0) {
            Results results=new Results("0","删除成功");
            return results;
        }
        else {
            Results results=new Results("1","删除失败");
            return results;

        }

    }
    @RequestMapping("/subjectSelect")
    @ResponseBody
    public Results subjectSelect(Subjects subjects) {
      List<Subjects> subjectsList =subjectService.selectSubjectBydel(subjects);
      return  new Results("0","查询成功",subjectsList);
    }
}
